<template>
  <div class="page-container padding-20 query-list-container">
    <div v-show="$route.name === 'memberMgt'">
      <div class="search-container">
        <el-form :inline="true" ref="form" :model="searchData" size="small">
          <el-form-item label="关键字输入">
            <el-input v-model="searchData.memberIdOrPhone" placeholder="用户ID或手机号"></el-input>
          </el-form-item>
          <el-form-item label="用户类别">
            <el-select v-model="searchData.memberType" clearable>
              <el-option v-for="(type, index) in memberTypeList" 
                :key="index" :value="type.value" :label="type.label">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="产品名称">
            <el-select v-model="searchData.productId" clearable>
              <el-option
                v-for="product in productList"
                :key="product.id"
                :label="product.name"
                :value="product.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-button type="primary" @click="onSearch" size="small">查询</el-button>
          <el-button @click="onReset" size="small">重置</el-button>
        </el-form>
      </div>
      <el-table class="mt-20 el-table-modify" :data="tableData" border>
        <el-table-column type="index" label="序号" align="center" width="50"></el-table-column>
        <el-table-column prop="memberId" align="center" label="用户ID"></el-table-column>
        <el-table-column prop="memberNickName" align="center" label="昵称">
          <template slot-scope="scope">
            <el-button size="mini" type="text"  @click="linkToDetail(scope.row)">{{scope.row.memberNickName}}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="memberPhone" align="center" label="手机号"></el-table-column>
        <el-table-column align="center" label="用户类型">
          <template slot-scope="scope">
            {{MEMBER_TYPE[scope.row.memberType]}}
          </template>
        </el-table-column>
        <el-table-column prop="brandName" align="center" label="权益品牌"></el-table-column>
        <el-table-column prop="saleWay" align="center" label="权益销售渠道"></el-table-column>
        <el-table-column prop="usedAmount" align="center" label="权益使用次数"></el-table-column>
        <el-table-column prop="memberRegisterDate" align="center" label="注册时间">
          <template slot-scope="scope">
            {{scope.row.memberRegisterDate | dateFrm}}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="72">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="linkEquityToDetail(scope.row)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        ref="pagination"
        class="mt-20"
        background
        :page-size="pageOption.pageSize"
        :total="pageOption.total"
        @size-change="onSizeChange"
        @current-change="onPageChange"
        layout="slot,->,prev,pager,next,sizes,jumper"
      >
        <span
          class="pagination-title"
        >共 {{pageOption.total}} 条记录 第 {{pageOption.page}} / {{pageOption.pageTotal}} 页</span>
      </el-pagination>
    </div>
    <router-view v-if="$route.name !== 'memberMgt'"></router-view>
  </div>
</template>

<script>
import {memberQuery} from '@/api/member';
import {productEnableAll} from '@/api/product';
import {MEMBER_TYPE} from '@/utils/const';

export default {
  name: "MemberMgt",
  components: {},
  data() {
    this.MEMBER_TYPE = MEMBER_TYPE

    return {
      memberTypeList: Object.keys(MEMBER_TYPE).map(item => {
        return {value: item, label: MEMBER_TYPE[item]};
      }),
      productList: [],
      searchData: {
        memberIdOrPhone: "",
        memberType: "",
        productId: ""
      },
      tableData: [],
      pageOption: {
        page: 1,
        pageSize: 20,
        total: 0,
        pageTotal: 1
      },
      selectData: null,
      visible: false
    };
  },
  methods: {
    onSearch() {
      this.pageOption.page = 1;
      this.requestData();
    },
    onReset() {
      this.pageOption.page = 1;
      this.searchData.memberIdOrPhone = '';
      this.searchData.memberType = '';
      this.searchData.productId = '';
      this.requestData();
    },
    linkToDetail(row) {
      this.$router.push({name: 'memberDetail', params: {id: row.memberId}});
    },
    linkEquityToDetail(row){
      this.$router.push({name: 'memberEquityDetail', params: {id: row.memberId}});
    },
    async requestData() {
      const {page, pageSize : size} = this.pageOption
      const postData = {};
      Object.keys(this.searchData).forEach(key => {
        let value = this.searchData[key];
        if ( value === false || value) {
          postData[key] = this.searchData[key]
        }
      });
      
      const {data, total} = await memberQuery({page: page - 1, size, ...postData});
      this.tableData = data;
      this.pageOption.total = total;
      this.pageOption.pageTotal = total ? Math.ceil(total / size) : 1
      
      if (this.$refs.pagination) {
        let childrenRefs = this.$refs.pagination.$children
        this.$nextTick(() => {
          childrenRefs.forEach(child => child.$forceUpdate())
        });
      }
    },
    onSizeChange(size) {
      this.pageOption.pageSize = size;
      this.requestData();
    },
    onPageChange(page) {
      this.pageOption.page = page;
      this.requestData();
    },
    async productEnableAll() {
      const {data} = await productEnableAll()
      this.productList = data;
    }
  },
  created() {
    this.requestData();
    this.productEnableAll()
  }
};
</script>

<style lang="scss">
</style>